﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UniRlv.COL;
using UniRlv.UI.Main.Widget;
using UniRlv.Utility;

namespace UniRlv.UI.Main.BasicInfo
{
    public partial class FrmAddOrEditProduct : UniRlv.UI.Main.DialogBase
    {
        ProductInfo info = null;

        public FrmAddOrEditProduct()
        {
            InitializeComponent();
        }

        private void FrmAddOrEditProduct_Load(object sender, EventArgs e)
        {
            if (this.Tag == null)
            {
                lstYXQDW.SelectedIndex = 1;
            }
            else
            {
                info = this.Tag as ProductInfo;
                ShowInfo(info);

                //不允许修改通用名和批准文号
                txtPZWH.ReadOnly = true;
                txtTYM.ReadOnly = true;
            }
        }

        void ShowInfo(ProductInfo item)
        {
            txtGG.Text = item.gg;
            txtPZWH.Text = item.pzwh;
            txtRemark.Text = item.remarks;
            txtTYM.Text = item.tym;
            numYXQ.Value = item.yxq;
            lstYXQDW.SelectedIndex = item.yxqdw;
            txtSpm.Text = item.spm;
            txtItemID.Text = item.itemid;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (info == null)
            {
                info = new ProductInfo();
                info.ID = DALOpr.GetNextObjectID("ProductInfo");
                info.company = PublicObject.CurCompany.name;
                info.createTime = DateTime.Now;
                info.creator = PublicObject.CurUser.name;
                info.state = 1;
            }

            info.gg = txtGG.Text;
            info.pzrq = DateTime.Now;
            info.pzwh = txtPZWH.Text;
            info.remarks = txtRemark.Text;
            info.tym = txtTYM.Text;
            info.yxq = Convert.ToInt32(numYXQ.Value);
            info.yxqdw = lstYXQDW.SelectedIndex;
            info.spm = txtSpm.Text;
            info.itemid = txtItemID.Text;

            this.Tag = info;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        private void btnGGEdit_Click(object sender, EventArgs e)
        {
            FrmMultiItemsEdit frm = new FrmMultiItemsEdit();
            frm.values = txtGG.Text;
            frm.Splitters = AppHelper.GetGGSplitter();
            frm.Splitter = "；";
            frm.Text = "编辑产品规格和编码";
            frm.Tip = "输入规格-产品编码";

            if(frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                txtGG.Text = frm.values;
            }
        }

        public override void OnScanCode(string orgCode, string validCode)
        {
            txtTYM.Text = AppHelper.SYZSGetTym(orgCode);
            txtPZWH.Text = AppHelper.SYZSGetPZWH(orgCode);
        }
        
    }
}
